/**
  ******************************************************************************
  * File Name          : produptsk.c
  * Description        : 产量数据上传
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#include "tskcfg.h"
#include "produp.h"
#include "gv.h"
#include <string.h>

#define _0d1g2t(x)     ((double)x/10000000.0)

static const char produp_data_format1[] = "KJ;CLSC|%02d;%02d;%03d;";       /* 市名;县名;矿名 */
static const char produp_data_format2[] = "%u-%02u-%02u/%02u:%02u:%02u;";  /* 年-月-日/时:分:秒 */
static const char produp_data_format3[] = "t;%.3f;%.3f;kg;%.3f;";          /* 小时产量;小时产能比;实时产量 */
static const char produp_data_format4[] = "t;%.3f;t;%.3f;t;%.3f;";         /* 日产量;月产量;年产量 */
static const char produp_data_format5[] = "tph;%.3f;mps;%.3f;zt;%04X ~||";      /* 流量;皮带速度;状态 */

/* 网络数据 组织 发送 接收 */
uint8_t produp_data_loop(Ethernet_SendQueStru *tbuf, Prod_DataBaseStru *pdb)
{
  uint16_t pos;
  uint32_t timeout;
  double temp;
  
  if((tbuf == NULL) || (pdb == NULL))
  {
    return 1;
  }

  pos = 0;
  pos += _snprintf((char *)(tbuf->buf+pos), (ETHERNET_TXBUF_MAXLEN-pos), produp_data_format1, \
                                                                         gv.hr.meas_pa.prod_city_num, \
                                                                         gv.hr.meas_pa.prod_county_num, \
                                                                         gv.hr.meas_pa.prod_mines_num);
  if(pos >= ETHERNET_TXBUF_MAXLEN){
    return 2;
  }

  pos += _snprintf((char *)(tbuf->buf+pos), (ETHERNET_TXBUF_MAXLEN-pos), produp_data_format2, \
                                                                         pdb->year, pdb->month, pdb->day, \
                                                                         pdb->hour, pdb->minute, pdb->second);
  if(pos >= ETHERNET_TXBUF_MAXLEN){
    return 3;
  }
  
  temp = _0d1g2t(pdb->hour_acc_0d1g);
  pos += snprintf((char *)(tbuf->buf+pos), (ETHERNET_TXBUF_MAXLEN-pos), produp_data_format3, \
                                                                        temp, temp/(double)gv.hr.meas_pa.prod_hour_capacity_t, \
                                                                        (double)pdb->interval_acc_0d1g/10000.0);
  if(pos >= ETHERNET_TXBUF_MAXLEN){
    return 4;
  }

  pos += _snprintf((char *)(tbuf->buf+pos), (ETHERNET_TXBUF_MAXLEN-pos), produp_data_format4, \
                                                                         _0d1g2t(pdb->day_acc_0d1g), _0d1g2t(pdb->month_acc_0d1g), \
                                                                         _0d1g2t(pdb->year_acc_0d1g));
  if(pos >= ETHERNET_TXBUF_MAXLEN){
    return 5;
  }

  pos += _snprintf((char *)(tbuf->buf+pos), (ETHERNET_TXBUF_MAXLEN-pos), produp_data_format5, \
                                                                         pdb->flowrate_kgh/1000.0, pdb->belt_speed_mms/1000.0, \
                                                                         pdb->state);
  if(pos >= ETHERNET_TXBUF_MAXLEN){
    return 6;
  }

  tbuf->len = pos;
  produp_recv_ok = 0;
  timeout = gv.hr.meas_pa.prod_net_timeout_s * 1000;
  ethif_tosend(tbuf);

  while(produp_recv_ok == 0)
  {
    vTaskDelay(1);
    if(timeout-- <= 0)
    {
      return 7;
    }
  }

  return 0;
}
